#include <QGridLayout>
#include <QMessageBox>
#include "tictactoe.h"


TicTacToe::TicTacToe(QWidget *parent)
    : QDialog(parent), item(Button::Empty), counter(0)
{
    QGridLayout *grid = new QGridLayout();
    setLayout(grid);
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            button[3*i + j] = new Button(this);
            connect(button[3*i + j], SIGNAL(buttonClick(Button *)),
                    this, SLOT(clickButton(Button *)));
            connect(button[3*i + j], SIGNAL(stateChanged()),
                    this, SLOT(changeStateButton()));
            grid -> addWidget(button[3*i + j], i, j);
        }
    }
}

void TicTacToe::clickButton(Button *button)
{
    if(button->getDrawItem() == Button::Empty)
    {
        button->setDrawItem(item);
        item = (item == Button::Cross ? Button::Nought : Button::Cross);
        counter++;
    }
    else
    {
        QMessageBox::information(
                this, "Wrong turn!",
                "This button has been already pushed!");
    }
}

void TicTacToe::changeStateButton()
{
    winner();
}

void TicTacToe::winner()
{
    QString message = QString::null;
    bool game = false;
    for(int i = 0; i < 3; i++)
    {
        if(button[i]->getDrawItem() == button[i + 3]->getDrawItem()
            && button[i + 3]->getDrawItem() == button[i + 6]->getDrawItem()
            && button[i]->getDrawItem() != Button::Empty
                    && button[i+3]->getDrawItem() != Button::Empty
                    && button[i+6]->getDrawItem() != Button::Empty)
            {
            message = (button[i]->getDrawItem() == Button::Cross
                       ? "Cross player won!" : "Nought player won!");
            game = true;
        }
        else if(button[i*3]->getDrawItem() == button[i*3 + 1]->getDrawItem()
            && button[i*3 + 1]->getDrawItem() == button[i*3 + 2]->getDrawItem()
            && button[i*3]->getDrawItem() != Button::Empty
                    && button[i*3+1]->getDrawItem() != Button::Empty
                    && button[i*3+2]->getDrawItem() != Button::Empty)
            {
            message= (button[i*3]->getDrawItem() == Button::Cross
                      ? "Cross player won!" : "Nought player won!");
            game = true;
        }
    }
    if(button[0]->getDrawItem() == button[4]->getDrawItem()
        && button[4]->getDrawItem() == button[8]->getDrawItem()
        && button[0]->getDrawItem() != Button::Empty
                && button[4]->getDrawItem() != Button::Empty
                && button[8]->getDrawItem() != Button::Empty)
        {
        message = (button[4]->getDrawItem() == Button::Cross
                   ? "Cross player won!" : "Nought player won!");
        game = true;
    }
    else if(button[2]->getDrawItem() == button[4]->getDrawItem()
        && button[4]->getDrawItem() == button[6]->getDrawItem()
        && button[2]->getDrawItem() != Button::Empty
                && button[4]->getDrawItem() != Button::Empty
                && button[6]->getDrawItem() != Button::Empty)
        {
        message= (button[4]->getDrawItem() == Button::Cross
                  ? "Cross player won!" : "Nought player won!");
        game = true;
    }

    if((game == true && message != QString::null) | (counter == 9))
    {
        QMessageBox msgBox;
        msgBox.setText("Game over");
        msgBox.setText(game ? message : "Friendship won!");
        msgBox.setInformativeText("Do you want to play again?");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::Yes);
        int ret = msgBox.exec();

        switch(ret)
        {
        case QMessageBox::Yes:
            this->play();
            break;
        case QMessageBox::No:
            this->close();
            break;
        default:
            this->close();
            break;
        }
    }


}

void TicTacToe::play()
{
    for(int i = 0; i < 9; ++i)
    {
        button[i]->setDrawItem(Button::Empty);
    }
    item = Button::Empty;
    counter = 0;
}

